package com.jock.zjaddressanddatepicker.pickerview;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.content.Context;

import com.jock.zjaddressanddatepicker.TimeUtil;
import com.jock.zjaddressanddatepicker.pickerview.dao.RegionDAO;
import com.jock.zjaddressanddatepicker.pickerview.view.OptionsPickerView;
import com.jock.zjaddressanddatepicker.pickerview.view.TimePickerView;

/**
 * 地址选择器
 * 
 * @author Jock.Lin
 * @since 2016-7-8 下午1:28:59
 */
public class OptionsPickerViewFactory {
	
	private  ArrayList<RegionInfo> item1;  //---- 省

	private  ArrayList<ArrayList<RegionInfo>> item2 = new ArrayList<ArrayList<RegionInfo>>(); //---- 市

	private  ArrayList<ArrayList<ArrayList<RegionInfo>>> item3 = new ArrayList<ArrayList<ArrayList<RegionInfo>>>(); //---- 区

	@SuppressWarnings("rawtypes")
	private OptionsPickerView pvOptions;
	private TimePickerView pvTime;;
	private OptionsPickerLitener optionsPickerLitener;
	private DateOptionsPickerLitener dateOptionsPickerLitener;
	
	public interface OptionsPickerLitener{
		void onPickerLitener(String province, String city, String district);
	}
	public interface DateOptionsPickerLitener{
		void onDatePickerLitener(String date);
	}

	/**
	 * 地址选择器
	 *
	 * @author
	 * 		Jock.Lin
	 * @since
	 * 		2016-7-8 下午1:41:55
	 * @param context
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public OptionsPickerView builtAddressOptionsPickerView(Activity context, OptionsPickerLitener optionsPickerLitener) {

		pvOptions = new OptionsPickerView(context);
		this.optionsPickerLitener = optionsPickerLitener;

		long times = System.currentTimeMillis();

		ArrayList<RegionInfo> tmp1 = (ArrayList<RegionInfo>) RegionDAO.getProvencesOrCity(context,1);
		ArrayList<RegionInfo> tmp2 = (ArrayList<RegionInfo>) RegionDAO .getProvencesOrCity(context,2);
		ArrayList<RegionInfo> tmp3 = (ArrayList<RegionInfo>) RegionDAO .getProvencesOrCity(context,3);

		/**
		 * 整理二级城市
		 */
		int len1 = tmp1.size();
		int len2 = tmp2.size();
		int len3 = tmp3.size();
		
		item1 = tmp1;
		for (int i = 0; i < len1; i++) {
			
			RegionInfo info 			= tmp1.get(i);
			ArrayList<RegionInfo> list1 = new ArrayList<RegionInfo>();
			 
			for (int j = 0; j < len2; j++) {
				
				RegionInfo info2 	  =  tmp2.get(j);
				if (info2.getParent() == info.getId()) {
					list1.add(info2);
				}
			}
			item2.add(list1);
		}

		/**
		 * 整理三级城市
		 */

		int len4 = item2.size();
		for (int i = 0; i < len4; i++) {

			ArrayList<RegionInfo> arrayList 	   = item2.get(i);
			ArrayList<ArrayList<RegionInfo>> list2 = new ArrayList<ArrayList<RegionInfo>>();

			int listLen = arrayList.size();
			for (int j  = 0; j < listLen; j++) {
				
				RegionInfo regionInfo       = arrayList.get(j);
				ArrayList<RegionInfo> list3 = new ArrayList<RegionInfo>();
				
				for (int k = 0; k < len3; k++) {
					
					RegionInfo info = tmp3.get(k);
					if (info.getParent() == regionInfo.getId()) {
						list3.add(info);
					}
				}
				list2.add(list3);
			}
			item3.add(list2);
		}
		pvOptions.setPicker(item1, item2, item3, true);
		pvOptions.setCyclic(true, false, false);
		pvOptions.setSelectOptions(4, 0, 0);
		
		pvOptions.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {

			@Override
			public void onOptionsSelect(int options1, int option2, int options3) {
				
				if (OptionsPickerViewFactory.this.optionsPickerLitener != null) {
					String item3Str = "";
					if (item3.get(options1).get(option2).size() > 0) {
						item3Str = item3.get(options1).get(option2).get(options3).getPickerViewText();
					}
					OptionsPickerViewFactory.this.optionsPickerLitener.onPickerLitener(item1.get(options1).getPickerViewText(),
							 item2.get(options1).get(option2).getPickerViewText(), item3Str);
					
				}
				
			}
		});
		System.err.println("spend times  ------------> = " + (System.currentTimeMillis() - times));

		return pvOptions;
	}
	
	/**
	 * 时间选择器
	 *
	 * @author
	 * 		Jock.Lin
	 * @since
	 * 		2016-7-8 下午3:26:15
	 * @param context
	 * @param dateOptionsPickerLitener
	 */
	public TimePickerView builtDateOptionsPickerView(Context context, DateOptionsPickerLitener dateOptionsPickerLitener){
		this.dateOptionsPickerLitener = dateOptionsPickerLitener;
		pvTime = new TimePickerView(context, TimePickerView.Type.YEAR_MONTH_DAY);
		Calendar calendar = Calendar.getInstance();
		pvTime.setRange(calendar.get(Calendar.YEAR) - 70, calendar.get(Calendar.YEAR));
		pvTime.setTime(new Date());
		pvTime.setCyclic(false);
		pvTime.setCancelable(true);
		pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener()
		{
			@Override
			public void onTimeSelect(Date date)
			{
				String timeStr = TimeUtil.millisToStringDate(date.getTime(), "yyyy年MM月dd日");
				OptionsPickerViewFactory.this.dateOptionsPickerLitener.onDatePickerLitener(timeStr);
			}
		});
		
		return pvTime;
	}
}
